package com.syht.wechatpay.tools;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.lang.reflect.Method;
import java.lang.reflect.Field;

/**
 * 对象工具类
 * 
 * @author PWWang
 * 
 */
public class ObjectTools {

	/**
	 * 根据属性名获取属性值
	 * */
	public static Object getFieldValueByName(String fieldName, Object o)
			throws Exception {

		String firstLetter = fieldName.substring(0, 1).toUpperCase();
		String getter = "get" + firstLetter + fieldName.substring(1);
		Method method = o.getClass().getMethod(getter, new Class[] {});
		Object value = method.invoke(o, new Object[] {});
		return value;
	}
	
	/**
	 * 根据属性名获取不为空的属性名
	 * */
	public static Object[] getNotNullFieldName(Object o)
			throws Exception {

		Field[] fields = o.getClass().getDeclaredFields();
		List<String> indexList = new ArrayList<String>();
		for (int i = 0; i < fields.length; i++) {
			if(null != getFieldValueByName(fields[i].getName(), o)){
				indexList.add(fields[i].getName());
			}
		}
		return indexList.toArray();
		
		
	}

	/**
	 * 获取属性名数组
	 * */
	public static String[] getFiledName(Object o) throws Exception {

		Field[] fields = o.getClass().getDeclaredFields();
		String[] fieldNames = new String[fields.length];
		for (int i = 0; i < fields.length; i++) {
			fieldNames[i] = fields[i].getName();
		}
		return fieldNames;
	}

	/**
	 * 获取属性类型(type)，属性名(name)，属性值(value)的map组成的list
	 * */
	public static List<Map<String, Object>> getFiledsInfo(Object o) throws Exception {

		Field[] fields = o.getClass().getDeclaredFields();
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> infoMap = null;
		for (int i = 0; i < fields.length; i++) {
			infoMap = new HashMap<String, Object>();
			infoMap.put("type", fields[i].getType().toString());
			infoMap.put("name", fields[i].getName());
			infoMap.put("value", getFieldValueByName(fields[i].getName(), o));
			list.add(infoMap);
		}
		return list;
	}

	/**
	 * 获取对象的所有属性值，返回一个对象数组
	 * */
	public static Object[] getFiledValues(Object o) throws Exception {
		String[] fieldNames = getFiledName(o);
		Object[] value = new Object[fieldNames.length];
		for (int i = 0; i < fieldNames.length; i++) {
			value[i] = getFieldValueByName(fieldNames[i], o);
		}
		return value;
	}
	
	/**
	 * 获取对象的不为空属性的属性名和值
	 * @param o
	 * @return
	 * @throws Exception
	 */
	public static String getFiledsNameAndValues(Object o) throws Exception{
		
		StringBuffer sb = new StringBuffer();
		Object [] filedArr = getFiledName(o);
		for (Object object : filedArr) {
			if("serialVersionUID".equals(object)){
				continue;
			}
			sb.append(object.toString());
			sb.append(":");
			
			if(null != getFieldValueByName(object.toString(), o)){
				sb.append(getFieldValueByName(object.toString(), o).toString());
			}else{
				sb.append("null");
			}
			sb.append(",");
		}
		return sb.toString();
	}
	
	/**
	 * 获取对象的不为空属性的属性名和值
	 * @param o
	 * @return
	 * @throws Exception
	 */
	public static HashMap<String, Object> getFiledsNameAndValuesByMap(Object o) throws Exception{
		
		HashMap<String, Object> hm = new HashMap<String, Object>();
		Object [] filedArr = getFiledName(o);
		for (Object object : filedArr) {
			if(null != getFieldValueByName(object.toString(), o)){
				
				hm.put(object.toString(), getFieldValueByName(object.toString(), o).toString());
			}
		}
		return hm;
	}
	
	/**
	 * 获取对象的不为空属性的属性名和值
	 * @param o
	 * @return
	 * @throws Exception
	 */
	public static String getNotNullFiledsNameAndValues(Object o) throws Exception{
		
		StringBuffer sb = new StringBuffer();
		Object [] filedArr = getNotNullFieldName(o);
		for (Object object : filedArr) {
			sb.append(object.toString());
			sb.append(":");
			sb.append(getFieldValueByName(object.toString(), o).toString());
			sb.append(",");
		}
		return sb.toString();
	}
	
}
